package stream;


import org.junit.Test;

/**
 * @author: feiwang_6
 * @create: 2018/12/20 17:03
 * @description:
 */
public class TestStream{
    
    @Test
    public void test(){
        System.out.println("372dd913-d8dc-4a0f-b6b2-7d6713781a9f".length());
    }
    
    public static void main(String[] args) {
        int[] arrayOne = new int[]{1, 2};
        int[] arrayTwo = new int[]{1, 5, 6, 10, 18, 20};
        System.out.println(foundMiddleEnum(arrayOne, arrayTwo));
        System.out.println(foundMiddleEnumByBinary(arrayOne, arrayTwo));
    }
    
    public static double foundMiddleEnumByBinary(int[] nums1, int[] nums2){
        int i = foundMin(nums1, nums2, (nums1.length + nums2.length + 1) / 2);
        int j = foundMin(nums1, nums2, (nums1.length + nums2.length + 2) / 2);
        return (i + j)/2.0;
    }
    
    public static int foundMin(int[] nums1, int[] nums2, int number){
        int remove1 = 0, remove2 = 0, result = 0;
        int half = number >>> 1;
        while (half > 0){
            int index1 = remove1 + half - 1;
            int index2 = remove2 + half - 1;
            
            //越界判断
            if(index1 > nums1.length - 1){
                half = nums2.length >>> 1;
                continue;
            }else if(index2 > nums2.length -1){
                half = nums1.length >>> 1;
                continue;
            }
            
            //比较
            if(nums1[index1] < nums2[index2]){
                remove1 += half;
            }else {
                remove2 += half;
            }
            half = number - (remove1 + remove2);
            half = half >>> 1;
        }
        int h = number - (remove1 + remove2);
        if(nums1.length > nums2.length){
            return nums1[remove1 + h -1];
        }else {
            return nums2[remove2 + h -1];
        }
    }
    

    public static double foundMiddleEnum(int[] nums1, int[] nums2){
        int mid = (nums1.length + nums2.length) >>> 1;
        int left = 0, right = 0;
        int i = 0, j = 0;
        while (mid >= 0){
            left = right;
            if(i < nums1.length && (j >= nums2.length || nums1[i] < nums2 [j])){
                right = nums1[i++];
            }else {
                right = nums2[j++];
            }
            mid--;
        }
        if((nums1.length + nums2.length) % 2 != 0){
            return right;
        }
        return (left + right) / 2.0;
    }
    
    
    public static void binaryInsertSort(int[] nums){
        int i = 1;
        while (i < nums.length){
            int left = 0, right = i - 1;
            int ai = nums[i];
            while (left <= right){
                int mid = (left + right) >>> 1;
                if(ai > nums[mid]){
                    left = mid + 1;
                }else {
                    right = mid - 1;
                }
            }
            //移动:从后向前
            for (int j = i; j >= left; j--){
                nums[j] = nums[j - 1];
            }
            nums[left] = ai;
            i++;
        }
    }
    
    public static void insertSort(int[] nums){
        for(int i = 1, j = 1; i < nums.length; j = ++i){
            int ai = nums[i];
            while (j > 0 && ai < nums[j-1]){
                //将有序序列后移一位
                nums[j] = nums[--j]; 
            }
            //将待排序ai元素插入有序数组中
            nums[j] = ai;
        }
    }
    
    public static int binarySearch(int[] nums, int target){
        int left = 0, right = nums.length - 1;
        while (left < right -1){
            int middle = (right + left) >>> 1;
            int midVal = nums[middle];
            if(target < midVal){
                right = middle;
            }else if(target > midVal){
                left = middle;
            } else {
                return middle;
            }
        }
        return -1; //key not found
    }
}